package b3.widget.warehouse
{
	import b3.config.*;
	import b3.model.*;
	import b3.net.*;
	import b3.util.*;
	import b3.widget.*;
	import b3.widget.basic.*;
	import b3.widget.tab.*;
	
	import flash.events.*;
	import flash.filters.*;
	import flash.text.*;
	import flash.display.*;
	
	public class Warehouse extends TabController
	{
		private static var instance:Warehouse;
		
		public function Warehouse()
		{
			var TabControllerBackground:Class = ResourceHelper.getInstance().getClassByName("WarehouseBgr");
			var TabTitleBackground:Class = ResourceHelper.getInstance().getClassByName("TabTitleBgr1");
			var HighlightTitleBgr:Class = ResourceHelper.getInstance().getClassByName("TabTitleBgr2");
			var CloseButton:Class = ResourceHelper.getInstance().getClassByName("CloseButton1");
			var TabPageFirstPage:Class = ResourceHelper.getInstance().getClassByName("TabPageFirstPage3");
			var TabPagePageUp:Class = ResourceHelper.getInstance().getClassByName("TabPagePageUp3");
			var TabPagePageDown:Class = ResourceHelper.getInstance().getClassByName("TabPagePageDown3");
			var TabPageLastPage:Class = ResourceHelper.getInstance().getClassByName("TabPageLastPage3");
			super(TabControllerBackground, CloseButton);
			this.anchor = BasicWidget.ANCHOR_MIDDLE_MIDDLE;
			var tab:Tab = new Tab("物品", 4, 2);
			tab.setPageSelectorStyle(TabPageFirstPage, TabPagePageUp, TabPagePageDown, TabPageLastPage, 0x66FF, 0x66FF);
			tab.setPageSelectorPos(190, 245);
			this.addTab(TabTitleBackground, tab.tabName, tab, HighlightTitleBgr);
			tab = new Tab("建筑", 4, 2);
			tab.setPageSelectorStyle(TabPageFirstPage, TabPagePageUp, TabPagePageDown, TabPageLastPage, 0x66FF, 0x66FF);
			tab.setPageSelectorPos(190, 245);
			this.addTab(TabTitleBackground, tab.tabName, tab, HighlightTitleBgr);
			tab = new Tab("邮件", 4, 2);
			tab.setPageSelectorStyle(TabPageFirstPage, TabPagePageUp, TabPagePageDown, TabPageLastPage, 0x66FF, 0x66FF);
			tab.setPageSelectorPos(190, 245);
			this.addTab(TabTitleBackground, tab.tabName, tab, HighlightTitleBgr);
			this.setTabTitlePos(52, -25); 
			this.setTabPagePos(84, 75);
			this.setCloseButtonPos(235, 353);
			this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			this.addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
		}

		private function onAddedToStage(event:Event):void
		{
			var me:Player = PlayerManager.getInstance().me; 
			me.packageModel.addEventListener(ModelEvent.REFRESH, onPackageRefresh);
			me.addEventListener(ModelEvent.LOAD_MAIL_COMPLETE, onMailRefresh);
			me.packageModel.loadObjects(false, true);
			me.loadMail(true);
			onPackageRefresh(null);
			onMailRefresh(null);
		}

		private function onRemovedFromStage(event:Event):void
		{
			var me:Player = PlayerManager.getInstance().me; 
			me.packageModel.removeEventListener(ModelEvent.REFRESH, onPackageRefresh);
			me.removeEventListener(ModelEvent.LOAD_MAIL_COMPLETE, onMailRefresh);
		}
		
		public static function getInstance():Warehouse
		{
			if (!instance)
				instance = new Warehouse();
			return instance;
		}
		
		private function onPackageRefresh(event:ModelEvent):void
		{
			var objectTab:Tab = tabs[0];
			var buildingTab:Tab = tabs[1]; 
			objectTab.removeAll();
			buildingTab.removeAll();
			var packageModel:PackageModel = PlayerManager.getInstance().me.packageModel;
			for (var str:String in packageModel.objectList)
			{
				var obj:Obj = packageModel.objectList[str];
				var objectConfig:ObjConfig = Config.objConfigs[obj.objectIndex];
				if (obj.objectNum > 0 && objectConfig.warehouseTabIndex >= 0)
				{
					var objectItem:ObjectItem;
					if (objectConfig is PermitConfig)
					{
						objectItem = new PermitItem(obj);
					}
					else
					{
						objectItem = new ObjectItem(obj);
					}
					tabs[objectConfig.warehouseTabIndex].addItem(objectItem, objectItem.width, objectItem.height);
				}
			}
		}
		
		private function onMailRefresh(event:ModelEvent):void
		{
			var me:Player = PlayerManager.getInstance().me;
			var tab:Tab = tabs[2];
			tab.removeAll();
			if (me.mails != null)
			{
				for (var i:int = 0,len:int = me.mails.length; i < len; i++)
				{
					var mail:MailModel = me.mails[i] as MailModel;
					var mailItem:MailItem = new MailItem(mail);
					tab.addItem(mailItem, mailItem.width, mailItem.height);
				}
			}
		}
		
		private function sortObjects(a:String, b:String):int
		{
			var objectConfigA:ObjConfig = Config.objConfigs[a];
			var objectConfigB:ObjConfig = Config.objConfigs[b];
			if (objectConfigA.index < objectConfigB.index)
			{
				return -1;
			}
			else if (objectConfigA.index > objectConfigB.index)
			{
				return 1;
			}
			return 0;
		}
		
	}
}